package com.yinhe.music.yhmusic.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.utils.ScreenUtils;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceViewPagerFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.constants.KeyConstants;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.search.adapter.MenuAdapter;
import com.yinhe.music.yhmusic.songmenu.SongMenuActivity;
import com.yinhe.music.yhmusic.songmenu.contract.ISongMenuListContract;
import com.yinhe.music.yhmusic.songmenu.presenter.SongMenuListPresenter;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;

/**
 * @author 何晓华
 * @date 2018/3/31
 */

public class MenuListFragment extends BaseServiceViewPagerFragment implements
        BaseQuickAdapter.OnItemClickListener, ISearchFragment, BaseQuickAdapter.RequestLoadMoreListener,
        ISongMenuListContract.ISongMenuListView {

    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView mRecyclerView;
    private String key;
    private MenuAdapter mAdapter;
    private int pageNum;
    private int mPageSize = 10;
    private int mPage = 1;
    private int mPageType;
    private String userId;

    private SongMenuListPresenter mPresenter;

    public static MenuListFragment newInstance(String key, int pageType, String userId) {
        MenuListFragment fragment = new MenuListFragment();

        Bundle bundle = new Bundle();
        bundle.putString("key", key);
        bundle.putInt(KeyConstants.KEY_PAGE_TYPE, pageType);
        bundle.putString(KeyConstants.KEY_USER_ID, userId);

        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SongMenuListPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void initView() {
        RxBus.get().register(this);

        if (getArguments() != null && TextUtils.isEmpty(key)) {
            key = getArguments().getString("key");
            mPageType = getArguments().getInt(KeyConstants.KEY_PAGE_TYPE, IConstants.SEARCH_SONG_MENU);
            userId = getArguments().getString(KeyConstants.KEY_USER_ID);
        }

        setList();
        initAdapter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.music_hall_recycler_layout;
    }


    @Override
    public void onDestroyView() {
        RxBus.get().unregister(this);
        super.onDestroyView();
    }

    @Override
    protected void onFragmentFirstVisible() {
        mPage = 1;
        reqMenuList();
    }

    private void setList() {
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, ScreenUtils.dp2px(20), false);
        mRecyclerView.addItemDecoration(itemDecoration);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    RxBus.get().post(RxBusEventType.Search.HIDE_SOFT_KEYBOARD, RxbusNullObject.INSTANCE);
                }
            }
        });
    }

    private void initAdapter() {
        mAdapter = new MenuAdapter(null, key);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.setPreLoadNumber(4);
        mAdapter.setOnItemClickListener(this);
    }

    private void reqMenuList() {
        if (mPageType == IConstants.SEARCH_SONG_MENU) {
            mPresenter.getSearchList(mPage, mPageSize, key);
        } else {
            mPresenter.getUserSongMenu(userId, mPage, mPageSize);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UmengEventUtils.clickShare(getActivity(), UmengEventUtils.SONG_MENU);
        SongMenuList menu = (SongMenuList) adapter.getData().get(position);
        Intent intent = new Intent(getActivity(), SongMenuActivity.class);
        intent.putExtra("menu", menu);
        intent.putExtra("menuid", menu.getSongMenuId());
        intent.putExtra("type", Music.Type.ONLINE);
        startActivity(intent);
    }

    @Override
    public void updateDataForKey(String key) {
        if (key.equals(this.key)) {
            return;
        }
        mPage = 1;
        this.key = key;
        if (mAdapter != null) {
            mAdapter.setSearchKey(key);
            reqMenuList();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        if (mPage <= pageNum) {
            reqMenuList();
        } else {
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    public void setSongMenuListUI(SongMenuList songMenuList) {
        List<SongMenuList> list = songMenuList.getSongMenuList();
        if (list == null) {
            return;
        }

        pageNum = songMenuList.getPageNum();

        if (mPage == 1) {
            mAdapter.setEmptyView(getEmptyView(mRecyclerView));
        }

        int size = list.size();

        if (mPage == 1) {
            mAdapter.setNewData(list);
        } else {
            if (size > 0) {
                mAdapter.addData(list);
            }
        }

        if (mPage >= pageNum) {
            mAdapter.loadMoreEnd();
        } else {
            mPage++;
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void setLoadMoreFailUI() {
        mAdapter.loadMoreFail();
    }
}
